package com.WFinstitute.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.WFinstitute.domain.Scholar;
import com.WFinstitute.service.ScholarService;
import com.WFinstitute.util.FileLoadUtil;
import com.WFinstitute.util.Page;
import com.WFinstitute.util.TimeUtil;


/**
 * 学者处理controller
 * @author 匹夫
 * @date 2016-7-25 下午11:31:23
 * @function
 */

@Controller
@RequestMapping("/scholarback")
public class ScholarBackController {
	@Resource
	private ScholarService  scholarService;
	
	/**
	 * 【后台页面】学者分页列表界面
	 * @param response
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/scholarList")
	public Map<String, Object> scholarList(HttpServletResponse response,HttpServletRequest request){
			//获取页面传来每页显示条数
	        int row=request.getParameter("rows")!=null?Integer.valueOf(request.getParameter("rows")):10;
	        //获取页面传来当前页码
	        int page=request.getParameter("page")!=null?Integer.valueOf(request.getParameter("page")):1;
	        //计算一共有多少条
	        int count = (int)scholarService.count();
			Page<Scholar> pageList = scholarService.findScholarList(row, page, "");
			List<Scholar> scholarList = pageList.getList();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("rows", scholarList);
			result.put("total", count);
			return result;
	}
	
	/**
	 * 【后台页面】添加学者以及修改学者
	 * @param response
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/addscholar")
	public void addscholar(HttpServletResponse response,HttpServletRequest request,ScholarBackController scholarBackController) throws IOException{
		 response.setCharacterEncoding("UTF-8");
		 
			JSONObject jo=new JSONObject();
		try {
		//图片上传路径
		String name = request.getParameter("name");
		String position = request.getParameter("position");
		String content = request.getParameter("content");
		String bookmaking = request.getParameter("bookmaking");
		//position= URLDecoder.decode(position,"utf-8");
		if(request.getParameter("id")!=null&&!request.getParameter("id").equals("")){
			int a = Integer.parseInt(request.getParameter("id"));
			Scholar scholar = scholarService.findScholarById(a);
			scholar.setDescription(content);
			scholar.setName(name);
			scholar.setPosition(position);
			scholar.setBookmaking(bookmaking);
			scholar.setCreatetime(TimeUtil.getCurrentTime());
			scholarService.updateScholar(scholar);
		}else{
			Scholar scholar=new Scholar();
			scholar.setDescription(content);
			scholar.setName(name);
			scholar.setPosition(position);
			scholar.setBookmaking(bookmaking);
			scholar.setCreatetime(TimeUtil.getCurrentTime());
			scholarService.addScholar(scholar);
		}
		jo.put("state", "0");
		response.getWriter().write(jo.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 【后台页面】修改学者头像
	 * @param response
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/updatescholarimg")
	public void addscholarimg(HttpServletResponse response,HttpServletRequest request,ScholarBackController scholarBackController) throws IOException{
		response.setCharacterEncoding("UTF-8");
		JSONObject jo=new JSONObject();
		try {
		//图片上传路径
        String img_url=FileLoadUtil.upImg(request, response,"1,1");
            int id=request.getParameter("id")!=null?Integer.valueOf(request.getParameter("id")):1;
			Scholar scholar = scholarService.findScholarById(id);
			//给图片地址加上<img>标签
			if(img_url!=null&&!img_url.equals("")){
			img_url="<img src="+img_url+"/>";
			scholar.setHeadimg(img_url);
			}
		scholarService.updateScholar(scholar);
		jo.put("state", "0");
		response.getWriter().write(jo.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 【后台页面】删除学者
	 * @param response
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/deletescholar")
	public void deletescholar(HttpServletResponse response,HttpServletRequest request,ScholarBackController scholarBackController) throws IOException{
            if(request.getParameter("id")!=null&&!request.getParameter("id").equals("")){
            	scholarService.deleteScholarById(Integer.parseInt(request.getParameter("id")));
            	JSONObject jo=new JSONObject();
            	jo.put("state", "0");
            	response.getWriter().print(jo);
            }
	}	
}
